package org.example.proxy.cglib;

import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;

/**
 * 若需要设置多个增强回调可考虑
 * 1、CompositeMethodInterceptor
 * 2、构建MethodInterceptor的链式调用
 */
public class CglibDynamicProxyDemo2 {
    public static void main(String[] args) {
        // 设置CGLIB调试信息输出目录
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "./target/cglib");

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(SomeClass.class);
        enhancer.setUseCache(false);
        enhancer.setUseFactory(false);

        Callback[] callbacks = new Callback[2];
        callbacks[0] = new CglibProxy();
        callbacks[1] = new CglibProxy2();

        enhancer.setCallbacks(callbacks);
        enhancer.setCallbackFilter(new MyCallbackFilter());

        SomeClass proxy = (SomeClass) enhancer.create();
        proxy.someMethod();
    }
}
